package zhenniu.best.webview.zhenniuwebview.utils;

import android.util.Base64;

import java.nio.charset.Charset;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 作者：xxy on 2018/11/2 15:22
 */
public class DecryptUtil {

    private static final String ENCODE = "UTF-8";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String AES = "AES";
    private static final String CHARSETNAME = "utf-8";
    public static final String DECRYPT_KEY = "ideal@(zhenniub)";


    public static String Ase(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec skeySpec = new SecretKeySpec(byteKey, "AES");
        cipher.init(opmode, skeySpec);
        byte[] decrypted = cipher.doFinal(byteData);
        return new String(decrypted, Charset.forName( "UTF8"));

    }


    /**
     * base64 解密
     * @param encodeWord 加密后的文字/比如密码
     */
    public static byte[] base64Decrypt(String encodeWord){
        return Base64.decode(encodeWord, Base64.NO_WRAP);
    }


    /**
     * 线base64然后aes
     * @param encodeWord
     * @param opmode
     * @return
     * @throws Exception
     */
    public static String base64Asedecrypt(String encodeWord, int opmode) throws Exception {
        byte[] decodeBase64 = Base64.decode(encodeWord, Base64.NO_WRAP);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecretKeySpec skeySpec = new SecretKeySpec(DECRYPT_KEY.getBytes(ENCODE), AES);
        cipher.init(opmode, skeySpec);
        byte[] decrypted = cipher.doFinal(decodeBase64);
        return new String(decrypted, Charset.forName( ENCODE));
    }
}
